﻿namespace TfsExtensions.Build.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using System.Collections.ObjectModel;
    using Microsoft.TeamFoundation.Framework.Common;
    using Microsoft.TeamFoundation;
    using System.Net;
    using System.Diagnostics;
    using Microsoft.TeamFoundation.Build.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
    using System.IO;
    using System.Reflection;
    using Microsoft.TeamFoundation.Server;
    using TfsExtensions.Build.Controls;

    public partial class DialogProvisionArtifacts : Form
    {
        private Collection<string> processTemplatesToProvision
        {
            get
            {
                return new Collection<string>
                                    {
                                        { "DefaultTemplateWithBuildAndDocumentation.xaml"},
                                    };
            }
        }

        string workspacePath;
        string invokerAssemblyLocation;
        string workspaceName = Guid.NewGuid().ToString();
        TfsTeamProjectCollection teamFoundationServer;
        VersionControlServer sourceControl;
        private string tfsServerName = "";
        string tfsProjectName = "";

        public DialogProvisionArtifacts(string serverName, string projectName, string invokerAssemblyLocation) 
        {
            InitializeComponent();
            this.tfsServerName = serverName;
            this.tfsProjectName = projectName;
            this.invokerAssemblyLocation = invokerAssemblyLocation;
            string[] dllfilePath = new string[1];
            workspacePath = Path.Combine(Path.GetTempPath(),workspaceName);
            txtSelectedProject.Text = tfsProjectName;
            
            new TfsHelper().GetTeamFoundationServerConnection(this.tfsServerName, this, out this.teamFoundationServer, out this.sourceControl);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (DialogBrowseServerFolder frmBrowse = new DialogBrowseServerFolder())
            {
                frmBrowse.SelectedPath = string.Empty;
                frmBrowse.TfsVersionControlServer = sourceControl;
                ICommonStructureService structureService = (ICommonStructureService)teamFoundationServer.GetService<ICommonStructureService>();
                frmBrowse.TfsCommonStructureService = structureService;

                ProjectInfo[] projects = structureService.ListAllProjects();
                frmBrowse.TfsServerName = tfsServerName;
                frmBrowse.SelectedPath = txtTemplatePath.Text; //"$/" + TFSProjectName;
                frmBrowse.ShowDialog();
                if (frmBrowse.DialogResult == DialogResult.OK)
                {
                    this.errorProvider1.Clear();
                    txtTemplatePath.Text = frmBrowse.SelectedPath;
                }
            }
        }

        private void provisionArtifacts_Load(object sender, EventArgs e)
        {
            new TfsHelper().DeleteWorkspace(workspaceName, workspacePath, this.teamFoundationServer, this.sourceControl);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtTemplatePath.Text))
            {
                this.errorProvider1.SetError(this.txtTemplatePath, "Please select the path to provision custom process template(s)");
                return;
            }

            try
            {
                this.UseWaitCursor = true;
                Workspace workspace = new TfsHelper().CreateWorkspace(workspaceName, txtTemplatePath.Text, workspacePath, this.teamFoundationServer, this.sourceControl);
                new TfsHelper().CheckoutOrAddFile(Path.Combine(workspacePath, processTemplatesToProvision[0]), workspace, ProvisionableResources.DefaultTemplateWithBuildAndDocumentation, false, this.sourceControl);
                TfsHelper.CheckInChanges(workspace, "Added/Updated custom process templates.");
                new TfsHelper().DeleteWorkspace(workspaceName, workspacePath, this.teamFoundationServer, this.sourceControl);
                this.UseWaitCursor = false;
                MessageBox.Show("Custom build process templates provisioned successfully. Please check the source control path for new template(s).");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occured during the operation." + ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

